<template>
  <view>

    <!-- 顶部自定义导航 -->
    <tn-nav-bar :isBack="false" :bottomShadow="false" backgroundColor="none">
      <view class="custom-nav tn-flex tn-flex-col-center tn-flex-row-center">
        <!-- 字 -->
        <view class="custom-nav__search tn-flex tn-flex-col-center tn-flex-row-center ">
          <view class="custom-nav__search__box tn-flex tn-flex-col-center tn-flex-row-center tn-color-black">
            <text class="tn-text-bold tn-text-xl">分组</text>
          </view>
        </view>
      </view>
    </tn-nav-bar>

    <view style="margin-top: 200rpx">
      <scroll-view v-if="groupList.length > 0" :scroll-y="true"
                   class="group-list">
        <view class="group-container">
          <view class="group-row">
            <view v-for="(item, index) in groupList" :key="index" class="group-item"
                  @click="goToGroupDetail(item)" @longpress="deleteOne(item)">
              <view>
                <view class="item-name">
                  {{ item.text }}
                </view>
              </view>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>



    <view>
      <!-- 提示窗示例 -->
      <uni-popup ref="alertDialog" type="dialog">
        <uni-popup-dialog type="error" cancelText="关闭" confirmText="删除" title="删除" :content="sureContent" @confirm="dialogConfirm"
        ></uni-popup-dialog>
      </uni-popup>
    </view>

    <view>
      <!-- 输入框示例 -->
      <uni-popup ref="inputDialog" type="dialog">
        <uni-popup-dialog ref="inputClose"  mode="input" title="输入分组名称" value=""
                          placeholder="请输入分组名称" @confirm="dialogInputConfirm"></uni-popup-dialog>
      </uni-popup>
    </view>

    <text class="tn-icon-add  tn-color-bluepurple add-grain" @click="goToAddNew"></text>


    <view class="tabbar footerfixed">
      <view class="action navbar__item -blue" @click="switchToHome">
        <view class="bar-icon">
          <view class="tn-icon-home-capsule navbar__icon">
          </view>
        </view>
        <view class="-blue bar-text">
          <text style="margin-bottom:-100rpx;font-size:20rpx;color:#303030;">首页</text>
        </view>
      </view>
      <view class="action navbar__item -orange" @click="switchToGroup">
        <view class="bar-icon">
          <view class="tn-icon-honor navbar__icon">
          </view>
        </view>
        <view class="-orange bar-text">
          <text style="margin-bottom:-100rpx;font-size:20rpx;color:#303030;">分组</text>
        </view>
      </view>
      <view class="action navbar__item -yellow" @click="switchToMemo">
        <view class="bar-icon">
          <view class="tn-icon-discover-planet navbar__icon">
          </view>
        </view>
        <view class="-yellow bar-text">
          <text style="margin-bottom:-100rpx;font-size:20rpx;color:#303030;">备忘</text>
        </view>
      </view>
      <view class="action navbar__item -purple" @click="switchToMy">
        <view class="bar-icon">
          <view class="tn-icon-my navbar__icon">
            <!-- <tn-badge :absolute="true">99+</tn-badge> -->
          </view>
        </view>
        <view class="-purple bar-text">
          <text style="margin-bottom:-100rpx;font-size:20rpx;color:#303030;">我的</text>
        </view>
      </view>
    </view>
    <view class="tn-padding-xl"></view>

  </view>
</template>

<script>
import {createGroup, groupList} from "@/api/group";
import {deleteOneGroup} from "@/api/group";
import {list} from "@/api/home";

export default {
  data() {
    return {
      deleteId: '',
      sureContent: '',
      groupList: [
      ],


    };

  },
  onLoad() {
    // #ifndef MP-WEIXIN
    uni.hideTabBar()
    // #endif
  },
  onShow() {
    groupList().then(res => {
      console.log('分组列表', res)
      this.groupList = res.data
    })
  },
  methods: {
    goToAddNew() {
      console.log('添加分组')
      this.$refs.inputDialog.open()
    },
    dialogInputConfirm(value){
      console.log('输入框确认', value)
      if(value===''){
        uni.showToast(
            {
              title: '分组名称不能为空',
              icon: 'none',
              duration: 2000
            }
        )
      }else{
        const request = {
          name: value
        }
        createGroup(request).then(res => {
          groupList().then(res => {
            console.log('分组列表', res)
            this.groupList = res.data
          })
        })
        this.$refs.inputDialog.close()
      }

    },
    dialogConfirm(){
      deleteOneGroup(this.deleteId).then(res => {
        groupList().then(res => {
          console.log('分组列表', res)
          this.groupList = res.data
        })
      })
    },
    deleteOne(item) {
      if(item.value === 1||item.value==='1'){
        uni.showToast(
            {
              title: '默认分组不能删除',
              icon: 'none',
              duration: 2000
            }
        )
        return
      }
      this.deleteId = item.value
      this.sureContent = '确定删除【'+item.text+'】吗？'
      this.$refs.alertDialog.open(item)
    },

    goToGroupDetail(item) {
      console.log('跳转到分组详情', item)
      //首先查询当前分组下有没有谷子，免得白跑一趟
      const request = {
        groupId: item.value
      }
      list(request).then(res => {
        console.log('当前分组下的谷子', res)
        if(res.data.length>0){
          uni.navigateTo({
            url: '/pages/group/grainList/grainList?groupId='+item.value
          })

        }else{
          uni.showToast(
              {
                title: '当前分组下没有谷子',
                icon: 'none',
                duration: 1000
              }
          )
        }
      })
    },
    switchToGroup(){
      console.log('切换到分组')
      uni.switchTab({
        url: '/pages/group/group'
      })
    },
    switchToHome(){
      console.log('切换到首页')
      uni.switchTab({
        url: '/pages/index/index'
      })
    },
    switchToMemo(){
      console.log('切换到备忘')
      uni.switchTab({
        url: '/pages/alarm/alarm'
      })
    },
    switchToMy(){
      console.log('切换到我的')
      uni.switchTab({
        url: '/pages/userInfo/userInfo'
      })
    },

  },
}
</script>



<style lang="scss" scoped>
@import '@/static/css/templatePage/custom_nav_bar.scss';
.template-music{
}

.group-list {
  width: 750rpx;
  //长度为剩余屏幕的高度
  height: calc(100vh - 300rpx);
  background-color: white;

  .group-container {
    display: flex;
    flex-wrap: wrap;
  }

  .group-row {
    display: flex;
    flex-wrap: wrap;
    width: 100%;
  }

  .group-item {
    width: 45%;
    height: 261rpx;
    margin-left: 24rpx;
    margin-bottom: 40rpx;
    border-radius: 8rpx;
    box-shadow: 0rpx 0rpx 50rpx 0rpx rgba(0, 0, 0, 0.12);
    background-color: #FFFFFF;
    border: 1rpx solid #E5E5E5;

    .item-name {

     //在这里添加样式，使得文字垂直居中、水平居中
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100%;
      font-size: 56rpx;
      color: #666666;
      line-height: 261rpx;

    }

  }

}

.itemm{
  margin-bottom: 50rpx;
  margin-left: 50rpx;
  width: 100%;
  height: 50rpx;
}

.label-class{
  padding: 5rpx 10rpx;
  border-radius: 20rpx;
  line-height: 45rpx;
  font-size: 24rpx;
  color: #82B2FF;
  background-color: #FFFFFF;
  box-shadow: 0rpx 0rpx 50rpx 0rpx rgba(0, 0, 0, 0.12);
}

.add-grain{
  position: fixed;

  bottom: 200rpx;
  right: 50rpx;
  font-size: 80rpx;

  z-index: 999;

}
/* 自定义导航栏内容 start */
.custom-nav {
  height: 100%;

  &__back {
    margin: auto 5rpx;
    font-size: 40rpx;
    margin-right: 10rpx;
    margin-left: 30rpx;
    flex-basis: 5%;
  }

  &__search {
    flex-basis: 60%;
    width: 100%;
    height: 100%;

    &__box {
      width: 100%;
      height: 70%;
      padding: 10rpx 0;
      margin: 0 30rpx;
      border-radius: 60rpx 60rpx 0 60rpx;
      font-size: 24rpx;
      background-color: rgba(255,255,255,0.2);
    }

    &__icon {
      padding-right: 10rpx;
      margin-left: 20rpx;
      font-size: 30rpx;
    }

    &__text {
    }
  }
}
/* 自定义导航栏内容 end */

/* 图标容器12 start */
.tn-three{
  position: absolute;
  top: 50%;
  right: 50%;
  bottom: 50%;
  left: 50%;
  transform: translate(-38rpx, -16rpx) rotateX(30deg) rotateY(20deg) rotateZ(-30deg);
  text-shadow: -1rpx 2rpx 0 #f0f0f0, -2rpx 4rpx 0 #f0f0f0, -10rpx 20rpx 30rpx rgba(0, 0, 0, 0.2);
}
.icon12 {
  &__item {
    width: 30%;
    background-color: #FFFFFF;
    border-radius: 10rpx;
    padding: 30rpx;
    margin: 20rpx 10rpx;
    transform: scale(1);
    transition: transform 0.3s linear;
    transform-origin: center center;

    &--icon {
      width: 100rpx;
      height: 100rpx;
      font-size: 60rpx;
      border-radius: 50%;
      margin-bottom: 18rpx;
      position: relative;
      z-index: 1;

      &::after {
        content: " ";
        position: absolute;
        z-index: -1;
        width: 100%;
        height: 100%;
        left: 0;
        bottom: 0;
        border-radius: inherit;
        opacity: 1;
        transform: scale(1, 1);
        background-size: 100% 100%;
        background-image: url(https://resource.tuniaokj.com/images/cool_bg_image/icon_bg.png);


      }
    }
  }
}
/* 文章内容 start*/
.tn-blogger-content {
  &__wrap {
    box-shadow: 0rpx 0rpx 50rpx 0rpx rgba(0, 0, 0, 0.12);
    border-radius: 20rpx;
    margin: 15rpx;
  }

  &__info {
    &__btn {
      margin-right: -12rpx;
      opacity: 0.5;
    }
  }

  &__label {
    &__item {
      line-height: 45rpx;
      padding: 0 20rpx;
      margin: 5rpx 18rpx 0 0;

      &--prefix {
        color: #82B2FF;
        padding-right: 10rpx;
      }
    }

    &__desc {
      line-height: 35rpx;
    }
  }

  &__main-image {
    border-radius: 16rpx 16rpx 0 0;

    &--1 {
      max-width: 690rpx;
      min-width: 690rpx;
      max-height: 400rpx;
      min-height: 400rpx;
    }

    &--2 {
      max-width: 260rpx;
      max-height: 260rpx;
    }

    &--3 {
      height: 212rpx;
      width: 100%;
    }
  }

  &__count-icon {
    font-size: 30rpx;
    padding-right: 5rpx;
  }
}

.image-music{
  padding: 150rpx 0rpx;
  font-size: 16rpx;
  font-weight: 300;
  position: relative;
}
.image-pic{
  background-size: cover;
  background-repeat:no-repeat;
  // background-attachment:fixed;
  background-position:top;
  border-radius: 20rpx 20rpx 0 0;
}
/* 文章内容 end*/

/* 底部tabbar start*/
.footerfixed{
  position: fixed;
  width: 100%;
  bottom: 0;
  z-index: 999;
  background-color: #FFFFFF;
  box-shadow: 0rpx 0rpx 30rpx 0rpx rgba(0, 0, 0, 0.07);
}

.tabbar {
  display: flex;
  align-items: center;
  min-height: 110rpx;
  justify-content: space-between;
  padding: 0;
  height: calc(110rpx + env(safe-area-inset-bottom) / 2);
  padding-bottom: calc(env(safe-area-inset-bottom) / 2);
}

.tabbar .action {
  font-size: 22rpx;
  position: relative;
  flex: 1;
  text-align: center;
  padding: 0;
  display: block;
  height: auto;
  line-height: 1;
  margin: 0;
  overflow: initial;
}

.tabbar .action .bar-icon {
  width: 100rpx;
  position: relative;
  display: block;
  height: auto;
  margin: 0 auto 10rpx;
  text-align: center;
  font-size: 42rpx;
}

.tabbar .action .bar-icon image {
  width: 50rpx;
  height: 50rpx;
  display: inline-block;
}

/* 底部标签栏动画 */
.navbar__item .bar-text {
  font-variant: small-caps;
  margin: 0;
  padding: 0;
  display: inline-flex;
  text-align: center;
  justify-content: center;
  align-items: center;
  white-space: nowrap;
  position: relative;
  width: 50rpx;
  height: 50rpx;
  background-size: cover;
  background-position: center;
  vertical-align: middle;
  background-color: currentColor;
  border-radius: 50%;
  transform: scale(0.8);
  opacity: 0;
  transition: all 0.55s cubic-bezier(0.71, 0.03, 0.23, 0.95);
}

.navbar__item.-blue {
  color: #06b8ff;
}

.navbar__item.-orange {
  color: #f2704d;
}

.navbar__item.-yellow {
  color: #f8cd4b;
}

.navbar__item.-purple {
  color: #8444d6;
}

.bar-text.-blue {
  color: #06b8ff;
}

.bar-text.-orange {
  color: #f2704d;
}

.bar-text.-yellow {
  color: #f8cd4b;
}

.bar-text.-purple {
  color: #8444d6;
}

.navbar__item:active .bar-text {
  transform: translateY(-74rpx) scale(1.4);
  opacity: 1;
  box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.1),
  -1px -1px 5px rgba(255, 255, 255, 1);
}

.navbar__item:active::before {
  opacity: 1;
}

.navbar__item:active::after {
  opacity: 0.4;
}

.navbar__item:active .navbar__icon {
  transform: translateY(-42rpx) scale(1);
  color: #fff;
  transition-delay: 0.1s, 0.1s;
}

.navbar__icon {
  bottom: -26rpx;
  transition: all 0.5s cubic-bezier(0.71, 0.03, 0.23, 0.95);
  transition-delay: 0.1s;
  display: inline-block;
  position: relative;
  z-index: 2;
}
</style>
